/*
 * Copyright (c) D.L.. All rights reserved.
 */

package xml;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * @author D.L.
 *
 */
public class KuGouSongTiltleExtracter {
	public static void main(String[] args) throws Exception, SAXException, IOException, ParserConfigurationException {
		File file=new File("E:/Projects/Java/CommonJavaWork/src/xml/tt.xsl");
		List<String> ns = extract(file);
		for (String n : ns) {
			System.out.println(new String(n.substring(0,n.lastIndexOf('.'))));
		}
	}
	static List<String> extract(File file) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException {
		XPath xpath = XPathFactory.newInstance().newXPath();
		XPathExpression xexpr = xpath.compile("/List/File/FileName");
		Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
		NodeList songtitles = (NodeList) xexpr.evaluate(doc, XPathConstants.NODESET);
		List<String> re = new ArrayList<>(songtitles.getLength());
		for (int j = 0; j < songtitles.getLength(); j++) {
			re.add(songtitles.item(j).getTextContent());
		}
		return re;
	}
}
